package com.loveyukari.service;

import com.loveyukari.common.exception.InternalException;
import com.loveyukari.dao.AuthDao;
import com.loveyukari.dao.PermissionDao;
import com.loveyukari.dao.TmpDao;
import com.loveyukari.dao.impl.BaseDao;
import com.loveyukari.model.Auth;
import com.loveyukari.model.Permission;
import com.loveyukari.utils.AliYunUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TmpService extends BaseDao{



    @Autowired
    private AuthDao authDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private AuthService authService;
    @Autowired
    private TmpDao tmpDao;


    Logger log = LoggerFactory.getLogger(TmpService.class);



    public List<Auth> getCarnivalAuth(){
        String sql = "select a.id from carnival c , auth a where c.douyu_name = a.name and c.start_time is not null";
        List<Integer> authIds = this.getJdbcTemplate().queryForList(sql, Integer.class);
        ArrayList<Auth> result = new ArrayList<>();
        for (Integer authId :
                authIds) {
            Auth auth = authService.getCacheAuth(authId);
            if (auth != null){
                result.add(auth);
            }
        }
        return result;


    }

    public List<Auth> drewCarnivalAuth(int size,String group,int blackId) throws InternalException{
        List<Auth> authList = this.getCarnivalAuth();
        int total = 0;
        for (Auth auth:
             authList) {
            total += auth.getFansLv();
        }
        Random random = new Random();
        ArrayList<Auth> result = new ArrayList<Auth>();

        Set<Integer> excludeSet = new HashSet<>();
        if (StringUtils.isNotEmpty(group)){
            excludeSet = tmpDao.selectAuthId(group);
        }
        if (size > authList.size() || authList.size() - excludeSet.size() < size ){
            throw new InternalException("参与抽奖人数过少");
        }
        if (blackId > 0 && !excludeSet.contains(blackId)){
            Auth blackAuth = authService.getCacheAuth(blackId);
            if (blackAuth != null){
                result.add(blackAuth);
            }
        }
        JSONArray logInfoAuths = new JSONArray();
        while(result.size() < size){
            int count = total;
            int i = random.nextInt(total);
            for (Auth auth :
                    authList) {
                count = count - auth.getFansLv();
                if (count <= i){
                    if(!excludeSet.contains(auth.getId())){
                        excludeSet.add(auth.getId());
                        result.add(auth);
                        JSONObject logInfoAuth = new JSONObject();
                        logInfoAuth.put("index",i);
                        logInfoAuth.put("authId",auth.getId());
                        logInfoAuths.add(logInfoAuth);
                    }
                    break;
                }
            }
        }
        if (StringUtils.isNotEmpty(group)){
            ArrayList<Integer> eids = new ArrayList<>();
            for (Auth auth :
                    result) {
                eids.add(auth.getId());
            }
            tmpDao.insertbatchDrawExclude(group,new ArrayList<Integer>(eids));
        }
        Collections.shuffle(result);
        JSONObject logInfo = new JSONObject();
        logInfo.put("total",total);
        logInfo.put("auths",logInfoAuths);
        log.info(logInfo.toString());
        return result;
    }




}
